import Vue from 'vue'
import App from './App.vue'
import router from "./router"
import store from "./store"
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import "../src/assets/css/base.css"
import "../src/assets/css/el-rest.css"
import subMenu from "qf-sub-menu"
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.use(subMenu);

// 路由守卫
router.beforeEach((to,from,next)=>{
  nProgress.start()
  let token = localStorage.getItem("qf-token") || ""
  if(token){
     if(store.state.sideMenu.length === 0){
       store.dispatch("GET_MENULIST")
       .then(()=>{
        next({path:to.path})
       })
     }else{
      next()
     }
    
  }else{
    if(to.path!=="/login"){
      next({path:"/login"})
    }else{
      next()
    }
  }

})

// 全局后置钩子
router.afterEach((to,from) =>{
  nProgress.done()
   let matched = [...to.matched]
   matched.splice(0,1)
   store.commit("SET_BREADCRUMB",matched)
})


new Vue({
  store,
  router,
  render: h => h(App),
}).$mount('#app')
